<script type="text/javascript">
    $(function() {
    //toggle for standalone and bonding selection
    $('#int1_sa, #int1_bond').click(function(){ 
        if ($('#int1_sa').is('input:checked')) {
            $('#int1_dev2').attr("disabled", true);
        }
        else {
            $('#int1_dev2').removeAttr("disabled", true);
        }
    });
    $('#int2_sa, #int2_bond').click(function(){ 
        if ($('#int2_sa').is('input:checked')) {
            $('#int2_dev2').attr("disabled", true);
        }
        else {
            $('#int2_dev2').removeAttr("disabled", true);
        }
    });

    //dhcp check
    $('#int1_bp, #int2_bp').click(function(){ 
        if ($('#int1_bp').is('input:checked')) {
            $('#int1_ip').attr("disabled", true);
            $('#int1_nm').attr("disabled", true);
        }
        else {
            $('#int1_ip').removeAttr("disabled", true);
            $('#int1_nm').removeAttr("disabled", true);
        }
        if ($('#int2_bp').is('input:checked')) {
            $('#int2_ip').attr("disabled", true);
            $('#int2_nm').attr("disabled", true);
        }
        else {
            $('#int2_ip').removeAttr("disabled", true);
            $('#int2_nm').removeAttr("disabled", true);
        }
    });

    //drop down validation, make sure the interface is not selected in some other dropdown
    $('#int1_dev1, #int1_dev2, #int2_dev1, #int2_dev2').change(function(event){ 
        var arr = Array("int1_dev1", "int1_dev2", "int2_dev1", "int2_dev2");
        for ( i=0; i< arr.length; i++ ) {
            var tmp = "#" + arr[i];
            var tmpi = $(tmp).get(0).selectedIndex;
            var tmpid = $(tmp).get(0).id;
            if ( (this.selectedIndex != 0) && (this.selectedIndex == tmpi) && (this.id != tmpid) ) {
                alert("The selected interface is already in use. Select a different interface");
                this.selectedIndex = 0;
                return false;
            }
        }
    });

    $('#int2_en').click(function(){ 
        if ($('#int2_en').is('input:checked')) {
            enable_int2();
        }
        else {
            disable_int2();
        }
    });
    });

    function disable_int2() {
        $("#int2_en").attr("checked", false);
        $("#int2_ip, #int2_nm, #int2_bp, #int2_sa, #int2_bond, #int2_dev1, #int2_dev2").attr("disabled", true);
    }

    function enable_int2() {
        $("#int2_en").attr("checked", true);
        $("#int2_ip, #int2_nm, #int2_bp, #int2_sa, #int2_bond, #int2_dev1, #int2_dev2").removeAttr("disabled", true);
    }

    function readDataFromHTML() {
        var data = new Object();
        data.interfaces = new Array();
        var num_int = 1;

        //if the 2nd interface is enabled add it
        if ($("#int2_en").is('input:checked')) {
            num_int = 2;
        }
        for ( i=0; i < num_int; i++ ) {
            var ni = new Object();

            ni.ports  = new Array();

            var int = "#int" + (i+1);
            var id_ip = int + "_ip";
            var id_nm = int + "_nm";
            var id_bp = int + "_bp";
            var id_dev1  = int + "_dev1";
            var id_dev2  = int + "_dev2";
            var id_sa = int + "_sa";
            var id_bond = int + "_bond";

            //standalone or bonding
            if ($(id_sa).is('input:checked')) {
                ni.mode = "standalone";
                ni.device = $(id_dev1).val();
                if ( $(id_dev1).val() === "" ) {
                    alert("For standalone you have to select the first interface");
                    return null;
                }
            }
            else if ($(id_bond).is('input:checked')) {
                ni.mode = "bonding";
                ni.device = "bond0";
                ni.ports[0] = $(id_dev1).val();
                ni.ports[1] = $(id_dev2).val();
                if ( ( $(id_dev1).val() === "" ) || ( $(id_dev2).val() === "" ) ) {
                    alert("For bonding you have to select both interfaces");
                    return null;
                }
            }
            else {
                alert("Please select Bonding or Standalone mode");
                return null;
            }

            //dhcp
            if ($(id_bp).is('input:checked')) {
                ni.bootproto = "dhcp";
            }
            else {
                ni.ipaddr = $(id_ip).val();
                ni.netmask = $(id_nm).val();
            }

            if ( (ni.bootproto != "dhcp") && (ni.ipaddr === "") && (ni.netmask === "") ) {
            }
            if ( ( $(id_dev1).val() === "" ) && ( $(id_dev1).val() === "" ) ) {
                continue;
            }

            data.interfaces.push(ni);
        }

        //forwarding
        if ($("#forwarding").is('input:checked')) {
            data.forwarding = 1;
        }
        else {
            data.forwarding = 0;
        }

        data.hostname = $("#hostname").val();
        data.gateway = $("#gateway").val();
        return data;
    }

    function addDropDown(data) {
        var tmp = "<option></option>";
        for ( i=0; i < data.devices.length; i++ ) {
            tmp += "<option>" + data.devices[i] + "</option>";
        }
        $('#int1_dev1, #int1_dev2, #int2_dev1, #int2_dev2').html(tmp);
    }

    function writeDataToHTML(data) {
        $("#hostname").val(data.hostname);
        $("#gateway").val(data.gateway);
        
        if ( data.forwarding === 1 ) {
            $("#forwarding").attr("checked", true);
        }
        else {
            $("#forwarding").attr("checked", false);
        }

        addDropDown(data);

        if ( data.interfaces.length === 1 ) {
            disable_int2();
        }
        else {
            enable_int2();
        }

        for ( i=0; i < data.interfaces.length; i++ ) {
            var int = "#int" + (i+1);
            var id_ip = int + "_ip";
            var id_nm = int + "_nm";
            var id_bp = int + "_bp";
            var id_dev1  = int + "_dev1";
            var id_dev2  = int + "_dev2";
            var id_sa = int + "_sa";
            var id_bond = int + "_bond";


            if ( data.interfaces[i].bootproto === "dhcp" ) {
                $(id_ip).attr("disabled", true);
                $(id_nm).attr("disabled", true);
                $(id_bp).attr("checked", true);
            }
            $(id_ip).val(data.interfaces[i].ipaddr);
            $(id_nm).val(data.interfaces[i].netmask);

            if ( data.interfaces[i].mode === "bonding" ) {
                $(id_dev1).attr("value", data.interfaces[i].ports[0]);
                $(id_dev2).attr("value", data.interfaces[i].ports[1]);
                $(id_dev1).removeAttr("disabled", true);
                $(id_dev2).removeAttr("disabled", true);
                $(id_bond).attr("checked", true);
            }
            else {
                $(id_dev1).attr("value", data.interfaces[i].device);
                $(id_dev2).attr("disabled", true);
                $(id_sa).attr("checked", true);
            }
        }
    }

    function validate(obj) {
        var msg = null;
        return msg;
    }

    function beforeSendFunction(msg) {
        $("#info").html("Setting up the network ... ");
        $("#info").dialog({
            width: 600,
            position: 'top',
            modal: true
        });
    }

    //called when the submit operation succeeds
    function successFunction(msg) {
        $("#info").html("Success: " + msg);
    }

    //called when the submit operation fails
    function errorFunction(msg) {
        $("#info").html("Error: " + msg);
    }

</script>
